<?php
namespace app\teacher\controller;
header("content-type:text/html;charset=utf-8");
use app\teacher\model\Setting;
use app\teacher\model\Text;
use app\teacher\model\User;
use think\Controller;
use think\Session;
use app\teacher\model\Teacher;


class Base extends Controller
{
    //初始化方法，相当于析构方法
    protected function _initialize(){
        header("content-type:text/html;charset=utf-8");
        header("Access-Control-Allow-Origin:*");
        header("Access-Control-Allow-Headers:Origin,X-Requested-With,Content-Type,Accept");
        header('Access-Control-Allow-Methods:GET,POST,PUT');
        ksort($_POST);
        ksort($_GET);
    }
     //判断用户是否存在
    public function user_info(){
        if(Session::has('teacher_infos')){
            return Teacher::get(Session::get("teacher_infos")["id"]);
        }else{
          $this->redirect("login/logins");
          exit();
        }
    }
    public function user_info2(){
      if(Session::has('user_infos')) {
        return User::get(Session::get("user_infos")["id"]);
      }
    }
    //获取setting
    public function getSetting($key){
        return Setting::where(array("k"=>$key))->find();
    }
    //获取所有的setting
    public function getAllSetting(){
        $ress = Setting::select();
        $ress = collection($ress)->toArray();
        $arr = array();
        foreach($ress as $k => $val){
            $arr[$ress[$k]['k']] = $val['val'];
        }
        return $arr;
    }
    //获取所有的Text
    public function getAllText(){
        $ress = Text::select();
        $ress = collection($ress)->toArray();
        $arr = array();
        foreach($ress as $k => $val){
            $arr[$ress[$k]['k']] = $val['val'];
        }
        return $arr;
    }
    //输出测试
    public function preEcho($data){
        echo "<pre>";
        var_dump($data);
        echo "</pre>";
        exit();
    }
    //输出测试
    public function preEcho2($data){
        echo "<pre>";
        var_dump($data);
        echo "</pre>";
    }
}
